package com.chinaums.sftp.utils;


import com.chinaums.sftp.internal.constants.ConstantValue;
import com.chinaums.sftp.internal.connection.SftpHandler;
import org.apache.flink.api.java.tuple.Tuple2;

import java.io.IOException;
import java.util.List;
import java.util.Map;


/**
 * file utils.
 */
public class FileUtil {


    public static void createFileInfo(
            List<Tuple2<String, Long>> subtaskFiles,
            String[] fileInfo,
            Map<String, Long> fileReadIndex) {

        Tuple2<String, Long> t2 = new Tuple2<>();
        t2.f0 = fileInfo[0];
        if (fileReadIndex != null && fileReadIndex.containsKey(fileInfo[0])) {
            if (fileReadIndex.get(fileInfo[0]) != 0
                    && fileInfo.length >= 2
                    && fileReadIndex.get(fileInfo[0]) < Long.parseLong(fileInfo[1])
            ) {
                t2.f1 = fileReadIndex.get(fileInfo[0]);
                subtaskFiles.add(t2);
            }
        } else {
            t2.f1 = 0L;
            subtaskFiles.add(t2);
        }
    }


    /**
     * add file for each subtasks.
     *
     * @param path
     * @param ftpHandler
     * @param files
     * @throws IOException
     */
    public static void addFiles(
            String path,
            SftpHandler ftpHandler,
            List<String> files) throws IOException {
        if (path != null && path.length() > 0) {
            if (!path.contains(ConstantValue.FILE_REGEX_SPLIT)) {
                path = path
                        .replace(ConstantValue.FILE_NEW_LINE, ConstantValue.EMPTY_REPLACE)
                        .replace(ConstantValue.FILE_NEW_TAB, ConstantValue.EMPTY_REPLACE)
                ;
                String[] pathArray = path.split(ConstantValue.FILES_SPLIT);  //  多个文件以逗号分隔
                for (String p : pathArray) {
                    files.addAll(ftpHandler.getFiles(p.trim()));
                }
            } else {
                path = path.replace(ConstantValue.FILE_NEW_LINE, "").replace(ConstantValue.FILE_NEW_TAB, ConstantValue.EMPTY_REPLACE);
                String dirPath = path.substring(0, path.lastIndexOf(ConstantValue.SINGLE_SLASH_SYMBOL) + 1);
                String fileReg = path.substring(path.lastIndexOf(ConstantValue.SINGLE_SLASH_SYMBOL) + 1)
                        .replaceAll(ConstantValue.FILE_REGEX_SPLIT, "");
                List<String> handlerFiles = ftpHandler.getFiles(dirPath);
                handlerFiles.forEach(file -> {
                    if (file.lastIndexOf(ConstantValue.FILE_REGEX_SPLIT) != -1 && file
                            .substring(
                                    file.lastIndexOf(ConstantValue.SINGLE_SLASH_SYMBOL) + 1,
                                    file.lastIndexOf(ConstantValue.FILE_REGEX_SPLIT))
                            .matches(fileReg)) {
                        files.add(file);
                    } else if (file
                            .substring(file.lastIndexOf(ConstantValue.SINGLE_SLASH_SYMBOL) + 1)
                            .matches(fileReg)) {
                        files.add(file);
                    }
                });
            }
        }
    }

}
